package com.pzb.goods.service.impl;

import java.beans.Transient;
import java.util.List;

import com.pzb.common.core.domain.entity.PicFile;
import com.pzb.goods.domain.*;
import com.pzb.goods.service.*;
import com.pzb.picture.service.IPicFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.goods.mapper.GdGoodsMapper;

/**
 * 商品管理Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2021-11-30
 */
@Service
public class GdGoodsServiceImpl implements IGdGoodsService 
{
    @Autowired
    private GdGoodsMapper gdGoodsMapper;
    @Autowired
    private IGdGoodsPictureService goodsPictureService;
    @Autowired
    private IPicFileService picFileService;
    @Autowired
    private IGdGoodsUnitsService goodsUnitsService;
    @Autowired
    private IGdGoodsAttributeValueService goodsAttributeValueService;

    /**
     * 查询商品管理
     * 
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public GdGoods selectGdGoodsById(Long id)
    {
        GdGoods goods = gdGoodsMapper.selectGdGoodsById(id);
        //查询商品图片
        List<PicFile> picFileList = picFileService.selectPicFileListByGoodsId(id);
        goods.setPicFileList(picFileList);
        //查询商品规格
        List<GdGoodsUnits> unitsList = goodsUnitsService.selectGdGoodsUnitsListByGoodsId(id);
        goods.setUnitsList(unitsList);
        //查询商品属性
        List<GdGoodsAttributeValue> attributeValueList = goodsAttributeValueService.selectGdGoodsAttributeValueListByGoodsId(id);
        goods.setAttributeValueList(attributeValueList);
        return goods;
    }

    /**
     * 查询商品管理列表
     * 
     * @param gdGoods 商品管理
     * @return 商品管理
     */
    @Override
    public List<GdGoods> selectGdGoodsList(GdGoods gdGoods)
    {
        return gdGoodsMapper.selectGdGoodsList(gdGoods);
    }

    /**
     * 新增商品管理
     * 
     * @param gdGoods 商品管理
     * @return 结果
     */
    @Override
    @Transient
    public int insertGdGoods(GdGoods gdGoods)
    {
        int count = gdGoodsMapper.insertGdGoods(gdGoods);
        //存储图片关联
        if (count >0 && gdGoods.getPicFileList()!=null){
            for (PicFile picFile : gdGoods.getPicFileList()) {
                GdGoodsPicture picture = new GdGoodsPicture();
                picture.setGoodsId(gdGoods.getId());
                picture.setPictureId(picFile.getId());
                goodsPictureService.insertGdGoodsPicture(picture);
            }
        }

        //新增商品规格属性值
        for (GdGoodsUnits gdGoodsUnits : gdGoods.getUnitsList()) {
            gdGoodsUnits.setExtAppid(gdGoods.getExtAppid());
            gdGoodsUnits.setGoodsId(gdGoods.getId());
            goodsUnitsService.insertGdGoodsUnits(gdGoodsUnits);
        }
        //新增商品属性
        for (GdGoodsAttributeValue attributeValue : gdGoods.getAttributeValueList()) {
            attributeValue.setGoodsId(gdGoods.getId());
            attributeValue.setExtAppid(gdGoods.getExtAppid());
            goodsAttributeValueService.insertGdGoodsAttributeValue(attributeValue);
        }
        return count;
    }

    /**
     * 修改商品管理
     * 
     * @param gdGoods 商品管理
     * @return 结果
     */
    @Override
    public int updateGdGoods(GdGoods gdGoods)
    {
        //删除图片关联
        goodsPictureService.deleteGdGoodsPictureByGoodsId(gdGoods.getId());
        //新增图片关联
        if (gdGoods.getPicFileList()!=null){
            for (PicFile picFile : gdGoods.getPicFileList()) {
                GdGoodsPicture picture = new GdGoodsPicture();
                picture.setGoodsId(gdGoods.getId());
                picture.setPictureId(picFile.getId());
                goodsPictureService.insertGdGoodsPicture(picture);
            }
        }
        //删除商品规格属性
        goodsUnitsService.deleteGdGoodsUnitsByGoodsId(gdGoods.getId());
        //新增商品规格属性值
        for (GdGoodsUnits gdGoodsUnits : gdGoods.getUnitsList()) {
            gdGoodsUnits.setExtAppid(gdGoods.getExtAppid());
            gdGoodsUnits.setGoodsId(gdGoods.getId());
            goodsUnitsService.insertGdGoodsUnits(gdGoodsUnits);
        }
        //删除商品属性
        goodsAttributeValueService.deleteGdGoodsAttributeValueByGoodsId(gdGoods.getId());
        //新增商品属性
        for (GdGoodsAttributeValue attributeValue : gdGoods.getAttributeValueList()) {
            attributeValue.setGoodsId(gdGoods.getId());
            attributeValue.setExtAppid(gdGoods.getExtAppid());
            goodsAttributeValueService.insertGdGoodsAttributeValue(attributeValue);
        }
        return gdGoodsMapper.updateGdGoods(gdGoods);
    }

    /**
     * 批量删除商品管理
     * 
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteGdGoodsByIds(Long[] ids)
    {
        return gdGoodsMapper.deleteGdGoodsByIds(ids);
    }

    /**
     * 删除商品管理信息
     * 
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteGdGoodsById(Long id)
    {
        return gdGoodsMapper.deleteGdGoodsById(id);
    }

    /**
     * 修改商品状态
     *
     * @param gdGoods 商品管理
     * @return 结果
     */
    @Override
    public int changeGoodsStatus(GdGoods gdGoods) {
        return gdGoodsMapper.updateGdGoods(gdGoods);
    }

    /**
     * 修改商品状态
     *
     * @param gdGoods 商品管理
     * @return 结果
     */
    @Override
    public int batchChangeGoodsStatus(GdGoods gdGoods) {
        return gdGoodsMapper.changeGoodsStatus(gdGoods);
    }

    @Override
    public int updateGoodsPriceStock(GdGoods gdGoods) {
        if(gdGoods.getUnitsList()!=null){
            for (GdGoodsUnits gdGoodsUnits : gdGoods.getUnitsList()) {
                goodsUnitsService.updateGdGoodsUnits(gdGoodsUnits);
            }
        }
        return gdGoodsMapper.updateGdGoods(gdGoods);
    }

    @Override
    public List<GdGoods> selectGdGoodsListByDiscountId(Long discountId) {
        return gdGoodsMapper.selectGdGoodsListByDiscountId(discountId);
    }

    @Override
    public List<GdGoods> selectGiftListByDiscountId(Long discountId) {
        return gdGoodsMapper.selectGiftListByDiscountId(discountId);
    }

    @Override
    public List<GdGoods> selectGdGoodsListByCouponId(Long CouponId) {
        return gdGoodsMapper.selectGdGoodsListByCouponId(CouponId);
    }

    @Override
    public List<GdGoods> selectPgPageItemGoodsListByItemId(Long itemId) {
        return gdGoodsMapper.selectPgPageItemGoodsListByItemId(itemId);
    }
}
